package cn.bonoon.kernel.menus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import cn.bonoon.kernel.menus.layouts.LayoutGenerable;
import cn.bonoon.util.PathUtil;

/**
 * 只有在channel下面才可以直接定义tabs，菜单是不允许定义的；
 * 而且在生成界面的时候，tabs是在菜单的后面的
 * 
 * @author jackson
 *
 */
public class TabsDefinition extends AbstractItemable implements Iterable<TabItemDefinition>, LayoutGenerable, ParentContainer{
	
	private final List<TabItemDefinition> items = new ArrayList<>();
	
	protected final ChannelDefinitionImpl channel;//相当于parent
	/** 可以导航的对象，显示在界面上的导航栏；导航栏可以根据自己需要定义不同的样式，只要界面支持 */
	protected MenuNavigation navigation;
	
	public TabsDefinition(ChannelDefinitionImpl channel){
		this.channel = channel;
		this.module = channel.module;
	}
	
	public void init(PlatformContext platformContext, ModuleContext moduleContext, ParentContainer parent){
		
		super.init(platformContext, moduleContext, parent);
		
		this.internalId = platformContext.combine(parent.getInternalId(), id);
		this.id = platformContext.idGenerate(internalId);
		this.path = platformContext.path("/s/sys/tabs");
		
		this.href = PathUtil.path(path, "!" + id, "index.do");
		
		for(TabItemDefinition tabItem : items){
			tabItem.init(platformContext, this);
			platformContext.add(tabItem);
		}
		Collections.sort(items);
		
		if(null != describe){
			this.describe.init(platformContext, this);
		}
	}

	public void button(StringBuilder a, String selected) {
		_button(href, a, selected);
	}

	@Override
	public String navigation() {
		return navigation.navigation();
	}

	public void add(TabItemDefinition tab){
		items.add(tab);
	}
	
	public boolean isEmpty() {
		return items.isEmpty();
	}

	@Override
	protected ChannelDefinitionImpl internalChannel() {
		return channel;
	}

	@Override
	public Iterator<TabItemDefinition> iterator() {
		return items.iterator();
	}
//
//	public DescribeDefinitionImpl getDescribe() {
//		return describe;
//	}
//
//	public void setDescribe(DescribeDefinitionImpl describe) {
//		this.describe = describe;
//	}
}
